package cn.iocoder.yudao.module.moa.controller.admin.jbapply;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.moa.controller.admin.jbapply.vo.*;
import cn.iocoder.yudao.module.moa.dal.dataobject.jbapply.JbApplyDO;
import cn.iocoder.yudao.module.moa.service.jbapply.JbApplyService;

@Tag(name = "管理后台 - MOA 加班申请")
@RestController
@RequestMapping("/moa/jb-apply")
@Validated
public class JbApplyController {

    @Resource
    private JbApplyService jbApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建MOA 加班申请")
    public CommonResult<Long> createJbApply(@Valid @RequestBody JbApplySaveReqVO createReqVO) {
        return success(jbApplyService.createJbApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新MOA 加班申请")
    public CommonResult<Boolean> updateJbApply(@Valid @RequestBody JbApplySaveReqVO updateReqVO) {
        jbApplyService.updateJbApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除MOA 加班申请")
    @Parameter(name = "id", description = "编号", required = true)

    public CommonResult<Boolean> deleteJbApply(@RequestParam("id") Long id) {
        jbApplyService.deleteJbApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得MOA 加班申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")

    public CommonResult<JbApplyRespVO> getJbApply(@RequestParam("id") Long id) {
        JbApplyDO jbApply = jbApplyService.getJbApply(id);
        return success(BeanUtils.toBean(jbApply, JbApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得MOA 加班申请分页")
    public CommonResult<PageResult<JbApplyRespVO>> getJbApplyPage(@Valid JbApplyPageReqVO pageReqVO) {
        PageResult<JbApplyDO> pageResult = jbApplyService.getJbApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JbApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出MOA 加班申请 Excel")

    @ApiAccessLog(operateType = EXPORT)
    public void exportJbApplyExcel(@Valid JbApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JbApplyDO> list = jbApplyService.getJbApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "MOA 加班申请.xls", "数据", JbApplyRespVO.class,
                        BeanUtils.toBean(list, JbApplyRespVO.class));
    }

}
